﻿using SF.Common;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

namespace SF.Ucenter.admin.statistics
{
    public partial class sourcerates : System.Web.UI.Page
    {
        protected string[] customer_rates = new string[] { "",""};
        protected void Page_Load(object sender, EventArgs e)
        {
            if (Session[DTKeys.SESSION_BUSNIESE_NUM] != null)
            {
                string businessNum = Session[DTKeys.SESSION_BUSNIESE_NUM].ToString(); 
                customer_rates = GetCustomerSourceRatesByBusinessNum(businessNum);
            }
        }

        protected string[] GetCustomerSourceRatesByBusinessNum(string businessNum)
        {
            BLL.statistics bll = new BLL.statistics();
            System.Data.DataSet dsSource = bll.GetCustomerSourceRatesByBusinessNum(businessNum);

            BLL.sf_appidmanage bll_a = new BLL.sf_appidmanage();
            System.Data.DataSet dsAppList = bll_a.GetList("businessNum='"+businessNum+"'");

            string [] arr_series = new string[2];
            string xAxis = string.Empty;
            string yAxis = string.Empty;

            foreach (System.Data.DataRow r in dsAppList.Tables[0].Rows) 
            {
                xAxis += "'"+r["appid_name"]+"',";
            }

            if (xAxis.Length > 0) 
            {
                xAxis = xAxis.Remove(xAxis.Length - 1, 1);

                string[] rateNames = new[] { "购买","未购买"};
                int[] rateTotalCount = new[] { 0, 0 };
                for (int i = 0; i < rateNames.Length; i++) 
                {
                    string temp_yAxis = string.Empty;
                    foreach (System.Data.DataRow r in dsAppList.Tables[0].Rows) 
                    {
                        System.Data.DataRow[] rows = dsSource.Tables[0].Select("rate='"+rateNames[i]+"' and appNum='"+r["appid"].ToString()+"'");
                        if (rows.Length > 0)
                        {
                            temp_yAxis += rows[0]["rates"].ToString() + ",";
                            rateTotalCount[i] += int.Parse(rows[0]["rates"].ToString());
                        }
                        else 
                        {
                            temp_yAxis += "0,";
                        }
                    }

                    if (temp_yAxis.Length > 0)
                    {
                        temp_yAxis = temp_yAxis.Remove(temp_yAxis.Length - 1, 1);
                    }

                    yAxis += "{type: 'column', name: '"+rateNames[i]+"人数', data: ["+temp_yAxis+"] },";
                }

                yAxis += "{size: 130,type: 'pie',name: '人数',data: [{name: '" + rateNames[0] + "',y: " + rateTotalCount[0] + "}, {name: '" + rateNames[1] + "',y: " + rateTotalCount[1] + "}]}";
            }

            arr_series[0] = xAxis;
            arr_series[1] = yAxis;
            return arr_series;
        }
    }
}